home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9766 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  85 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!jodell
  3. From: jodell@netcom.com (Jake Odell)
  4. Subject: Re: Help !!!!! on 'strstr"
  5. Message-ID: <jodellDo7097.Bto@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <314703FF.4045@msmail.st.stems.com>
  9. Date: Wed, 13 Mar 1996 06:19:54 GMT
  10. Sender: jodell@netcom20.netcom.com
  11.  
  12. kim hai (Kim_Hai.Ng@msmail.st.stems.com) posted:
  13. # Help !!!!!
  14.  
  15. # Problem :
  16. #     I need to locate a string within another string, and the process 
  17. # must be case insensitive. As far as I know, strstr() only locates the 
  18. # exact match and is case sensitive.
  19. #     Does anyone konw of any functions that could allow me to locate 
  20. # substrings and is case insensitive ?
  21.  
  22. # Eg.
  23. # case 1:
  24. #     string1 = "abc"
  25. #     string2 = "abcdef" 
  26. # case 2:
  27. #     string2 = "Abcdef"
  28. # case 3:
  29. #     string3 = "aBcdef"
  30. # case 4:
  31. #     string4 = "abCdef"
  32. # ...
  33. # ..
  34. # .
  35.  
  36. Here is istrstr() that came out of
  37. a similar thread a while back:
  38.  
  39. /*
  40.  * istrstr.c
  41.  */
  42. #if defined(DEBUG)
  43. # include <stdio.h>
  44. #endif
  45. #include <ctype.h>
  46.  
  47. typedef unsigned char uchar;
  48.  
  49. char *istrstr(s1, s2) char *s1; char *s2;
  50. {    char    *p, *ret;
  51.     int        len1, len2, matches;
  52.     int        c1, c2;
  53.  
  54.     for (len1 = 0; s1[len1]); len1++);
  55.     for (len2 = 0; s2[len2]); len2++);
  56.     if (len2 > len1) return NULL;
  57.     p = ret = s1;
  58.     while (*p)
  59.     {
  60. #if defined(DEBUG)        
  61.         c1 = *p;
  62.         c2 = s2[matches];
  63.         printf("%c/%02x %c/%02x", c1, c1, c2, c2);
  64. #endif
  65.         if ((c1 = toupper((uchar) *p++))
  66.         ==  (c2 = toupper((uchar) s2[matches++])))
  67.         {
  68. #if defined(DEBUG)
  69.             printf("\n");
  70. #endif
  71.             if (matches == len2) return ret;
  72.             continue;
  73.         }
  74.         ret = p;
  75.         if (matches = (c1 == toupper((uchar) *s2)) ? 1 : 0) ret--;
  76. #if defined(DEBUG)
  77.         printf("%s\n", matches ? " <-rewind s2 match" : "");
  78. #endif
  79.     }
  80.     return s2[matches] ? NULL : ret;
  81. }
  82.  
  83. -- 
  84. jake@pantheon.us.com   jodell@netcom.com
  85.